package com.haha;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class HekiliSkillDetector {

    // Hekili图标在屏幕上的位置和尺寸（根据实际调整）
    private static final int FIRST_ICON_X = 0;
    private static final int FIRST_ICON_Y = 0;
    private static final int ICON_WIDTH = 64;
    private static final int ICON_HEIGHT = 75;
    private static final int ICON_SPACING = 64;

    // 预加载技能图标模板
    private static final Map<String, BufferedImage> skillTemplates = new HashMap<>();

    static {
        // 实际使用时替换为你的技能图标文件路径
        loadTemplate("fireball", "E:\\IdeaProjects\\haha-maven\\src\\main\\resources\\templates\\ability_demonhunter_soulcleave.png");
        loadTemplate("deathvortex", "E:\\IdeaProjects\\haha-maven\\src\\main\\resources\\templates\\spell_deathvortex.png");
        // 添加更多技能...
    }

    private static void loadTemplate(String name, String path) {
        try {
            BufferedImage img = ImageIO.read(new File(path));
            skillTemplates.put(name, img);
        } catch (Exception e) {
            System.err.println("加载模板失败: " + path);
        }
    }

    public static void main(String[] args) throws Exception {
        Point mouseLoc = MouseInfo.getPointerInfo().getLocation();
        System.out.println("当前坐标: X=" + mouseLoc.x + " Y=" + mouseLoc.y);

        // 1. 捕获游戏窗口区域
        Rectangle captureArea = new Rectangle(
                FIRST_ICON_X,
                FIRST_ICON_Y,
                ICON_WIDTH * 3, // 捕获前3个技能图标
                ICON_HEIGHT
        );

        BufferedImage screenshot = new Robot().createScreenCapture(captureArea);

        // 2. 检测技能序列
        System.out.println("检测到技能序列：");
        for (int i = 0; i < 3; i++) {
            BufferedImage icon = screenshot.getSubimage(
                    i * ICON_SPACING,
                    0,
                    ICON_WIDTH,
                    ICON_HEIGHT
            );

            String skill = identifySkill(icon);
            System.out.println("位置 " + (i+1) + ": " + (skill != null ? skill : "未知技能"));
        }

        // 保存截图供调试
        ImageIO.write(screenshot, "png", new File("hekili_snapshot.png"));
    }

    private static String identifySkill(BufferedImage icon) {
        String closestMatch = null;
        double minDiff = Double.MAX_VALUE;

        // 遍历所有模板进行对比
        for (Map.Entry<String, BufferedImage> entry : skillTemplates.entrySet()) {
            double diff = calculateImageDifference(icon, entry.getValue());

            // 找到最接近的匹配
            if (diff < minDiff) {
                minDiff = diff;
                closestMatch = entry.getKey();
            }
        }

        // 设置相似度阈值（根据实际调整）
        return minDiff < 15.0 ? closestMatch : null;
    }

    /**
     * 计算图像差异度（平均像素差异）
     */
    private static double calculateImageDifference(BufferedImage img1, BufferedImage img2) {
        System.out.println("img1.getWidth()=="+img1.getWidth());
        System.out.println("img2.getWidth()=="+img2.getWidth());
        if (img1.getWidth() != img2.getWidth() || img1.getHeight() != img2.getHeight()) {
            System.out.println("直接返回...");
            return Double.MAX_VALUE;
        }

        long diff = 0;
        for (int y = 0; y < img1.getHeight(); y++) {
            for (int x = 0; x < img1.getWidth(); x++) {
                int rgb1 = img1.getRGB(x, y);
                int rgb2 = img2.getRGB(x, y);

                int rDiff = Math.abs((rgb1 >> 16) & 0xFF - (rgb2 >> 16) & 0xFF);
                int gDiff = Math.abs((rgb1 >> 8) & 0xFF - (rgb2 >> 8) & 0xFF);
                int bDiff = Math.abs(rgb1 & 0xFF - rgb2 & 0xFF);

                diff += rDiff + gDiff + bDiff;
            }
        }
        return diff / (double)(img1.getWidth() * img1.getHeight() * 3);
    }

}
